package dhy.com.tomcat.three.request.utils;

import dhy.com.tomcat.three.param.ParameterMap;

import javax.servlet.http.Cookie;

/**
 * @author 大忽悠
 * @create 2022/3/10 21:23
 */
public class RequestUtil {

    /**
     * 解析请求参数---url--->?后面拼接的请参数
     * TODO:请求参数按照指定编码解析,有待改进
     */
    public static void parseParameters(ParameterMap results, String queryString, String encoding) {
        String[] strings = queryString.split("&");
        for (int i = 0; i < strings.length; i++) {
            String[] split = strings[i].split("=");
            String key = split[0];
            String value = split[1];
            results.put(key,value);
        }
    }

    /**
     * 解析cookie
     * Cookie: BD_UPN=1d314753; ispeed_lsm=4; sugstore=1;
     */
    public static Cookie[] parseCookieHeader(String value) {
        String[] s = value.split("; ");
        Cookie[] cookies=new Cookie[s.length];
        for (int i = 0; i < s.length; i++) {
            String[] split = s[i].split("=");
            String key = split[0];
            String val = split[1];
            Cookie cookie = new Cookie(key, val);
            cookies[i]=cookie;
        }
        return cookies;
    }
}
